﻿using Autofac;
using Microsoft.Extensions.Logging;
using Quartz;
using System;
using System.Threading.Tasks;

namespace Infrastructure.Job
{
    public abstract class AbstractJob : IJob
    {
        public ILogger<AbstractJob> Logger { get; set; }

        public Task Execute(IJobExecutionContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                ServiceProvide.DoLifeScope(ls =>
                {
                    ls.InjectProperties(this);
                    try
                    {
                        Run();
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(ex);
                    }
                });
            });
        }


        public abstract void Run();
    }
}
